<?php

header('Content-Type:text/html;charset=utf-8'); // 设置文件编码方式
header('Access-Control-Allow-Origin: http://127.0.0.1:5500'); // CORS允许跨域


$servername = '127.0.0.1:3306';
$username = $password = 'root';
$dbname = '1907';

$conn = mysqli_connect($servername, $username, $password, $dbname);

if(!$conn) {
  die('数据连接失败');
}

$username = $_GET["username"];
$password = $_GET["password"];
$telephone = $_GET["telephone"];


$sql = "select count(*) from userinfo WHERE username = '$username'";

$result = mysqli_query($conn, $sql);

$data = mysqli_fetch_array($result);


if($data["0"] > 0) {
  // 存在
  $obj = array();

  $obj["code"] = 0;
  $obj["msg"] = '用户已存在';
  $obj["data"] = false;

  echo json_encode($obj);

} else {


  // $sql = "INSERT TO userinfo(username, password, telephone) VALUES ('$username', '$password', '$telephone')"

  $sql = "INSERT INTO userinfo(username, password, telephone) VALUES ('$username', '$password', '$telephone')";

  mysqli_query($conn, $sql);

  $count = mysqli_affected_rows($conn);

  // echo $count;

  if($count) {
    // 插入成功

    $obj = array();

    $obj["code"] = 1;
    $obj["msg"] = '注册成功';
    $obj["data"] = true;

    echo json_encode($obj);
  }

}

// echo json_encode($data["0"]);









